//
//  FLLFoodMaterialViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/13.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLFoodMaterialViewController.h"
#import "FLLMacro.h"
#import "FLLFoodMaterialModel.h"
#import "FoodMaterialData.h"
#import "FLLFoodMaterialTableViewCell.h"
#import "FLLFoodMatreialNameTableViewCell.h"
#import "FoodMaterial.h"
#import "FoodMaterialSpices.h"

@interface FLLFoodMaterialViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *foodMaterialTableView;
@property (nonatomic, strong) FLLFoodMaterialModel *fllFoodMaterialModel;
@property (nonatomic, strong) NSArray<FoodMaterial *> *materialArray;
@property (nonatomic, strong) NSArray<FoodMaterialSpices *> *spicesArray;
@end

@implementation FLLFoodMaterialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self createPOSTRequest];
    [self createFoodMaterialTableView];
  
}
- (void)createPOSTRequest
{
    
    NSString *bodyString = [NSString stringWithFormat:@"methodName=DishesMaterial&dishes_id=%@&version=4.40&", _idString];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
      
        self.fllFoodMaterialModel = [FLLFoodMaterialModel modelWithDic:result];
        self.materialArray = self.fllFoodMaterialModel.data.material;
        self.spicesArray = self.fllFoodMaterialModel.data.spices;
        [self.foodMaterialTableView reloadData];
       
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
}
- (void)createFoodMaterialTableView
{
    
    self.foodMaterialTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 95 * kHeightScale) style:UITableViewStylePlain];
    self.foodMaterialTableView.delegate = self;
    self.foodMaterialTableView.dataSource = self;
    self.foodMaterialTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.foodMaterialTableView.bounces = NO;
    [self.foodMaterialTableView registerNib:[UINib nibWithNibName:@"FLLFoodMaterialTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"foodMaterialTableCell"];
    [self.foodMaterialTableView registerNib:[UINib nibWithNibName:@"FLLFoodMatreialNameTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"foodMaterialNameCell"];
    [self.view addSubview:_foodMaterialTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    

    if (section == 0) {
        return 1;
    } if (section == 1) {
        return _materialArray.count;
    }
    return _spicesArray.count;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
       if (indexPath.section == 1) {
        
        FLLFoodMatreialNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodMaterialNameCell"];
           cell.materialName.text = _materialArray[indexPath.row].material_name;
           cell.materialWeight.text = _materialArray[indexPath.row].material_weight;
           cell.materialName.font = [UIFont systemFontOfSize:14 *kWidthScale];
           cell.materialWeight.font = [UIFont systemFontOfSize:14 *kWidthScale];

        return cell;
       }
        FLLFoodMaterialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodMaterialTableCell"];
      if (indexPath.section == 0) {
        [cell.materialImage sd_setImageWithURL:[NSURL URLWithString:self.fllFoodMaterialModel.data.material_image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        cell.blackImage.image = nil;
        cell.materialName.text = nil;
      } if (indexPath.section == 2) {
        [cell.materialImage sd_setImageWithURL:[NSURL URLWithString:_spicesArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        cell.blackImage.image = [UIImage imageNamed:@"detailbg"];
        cell.materialName.text = _spicesArray[indexPath.row].title;
        cell.materialName.font = [UIFont systemFontOfSize:14 *kWidthScale];
      }
    
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 1 ) {
        
        return  [GetHeightTools heightWith:_materialArray[indexPath.row].material_name] + 20 * kHeightScale;
    }
    return SCREEN_WIDTH * 2 / 3.0;
}

- (void)setIdString:(NSString *)idString
{

    _idString = idString;
    [self.foodMaterialTableView reloadData];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
